//获取应用实例
//获取应用实例
import network from '../../utils/network.js';
const app = getApp()
const baseUrl = 'http://jbc.com';

Page({
 
    /**
     * 页面的初始数据
     */
    data: {
      show: false,//控submit制下拉列表的显示隐藏，false隐藏、true显示
      selectData: [],//下拉列表的数据
      index: 0,//选择的下拉列表下标
      showid:0,
      uid:'',
      role_id:0,
      shop_id:0,
      zhuangt:false,
      tihuoWay:'',
      manager_id:0, 
      shop_flow:0,
      manager_flow:0,
      sale_flow:0,
      list:[],
      sale:[],
    },
  // 点击下拉显示框
  selectTap() {
    this.setData({
      show: !this.data.show
    });
  },
  // 点击下拉列表
  optionTap(e) {
    var that = this;
    let Index = e.currentTarget.dataset.index;//获取点击的下拉列表的下标
    this.setData({
      index: Index,
      manager_id: that.data.selectData[Index].id,
      show: !this.data.show
    });
    that.getsale_list("加载列表"); 
  },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      var that = this;
      var user = wx.getStorageSync('user');
      if(user.role_id == 1){
        that.setData({
          uid:user.id, 
          role_id: user.role_id,
          shop_id: user.shop_id,          
        });  
        that.getManager_list("加载列表"); 
      }else{
        that.setData({
          manager_id: user.user_id,
          role_id: user.role_id,
          selectData:[{id:user.role_id,name:user.name}]
        });
        that.getsale_list("加载列表"); 
      } 
      
    },

  //获取业务经理列表
  getManager_list: function (message) {
    var that = this;
    var data = {
      role_id: that.data.role_id,
      shop_id: that.data.shop_id,
      ischeck:1
    };
    network.requestLoading(baseUrl+'/System/User/manager_list', data, message, function (res) {
      if (res.code > 0) {
         that.setData({
           manager_id:res.data.list[0].id,
           selectData:res.data.list
         });
        that.getsale_list("数据加载中");
      } else {
        wx.showToast({
          title: res.msg,
        })
      }
    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
      })

    })
  },


  //获取业务员列表
  getsale_list: function (message) {
    var that = this;
    var data = {
      manager_id: that.data.manager_id,
      ischeck: 1
    };
    network.requestLoading(baseUrl+'/System/User/sale_flow_list', data, message, function (res) {
      if (res.code > 0) { 
        var ids = [];
        for (var i = 0; i < res.data.list.length;i++){
          ids[i] = res.data.list[i].id;
        };
        that.setData({
          sale: ids,
          shop_flow:res.data.shop_flow,
          manager_flow: res.data.manager_flow,
          sale_flow: res.data.sale_flow,
          list: res.data.list
        });
      } else {
        wx.showToast({
          title: res.msg,
        })
      }
    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
      })

    })
  },
  //全选
  election:function(e){
    var that = this;
    var show = e.currentTarget.dataset.showid;  
    if (show == 0){
      that.setData({
        showid: 1,
        zhuangt: 1,
      });

    } else if (show == 1){
      that.setData({
        showid: 0,
        zhuangt: 0,
      }); 
    } 

  },

  // 提交表单
    formSubmit: function (e) {
      var that = this;  
      wx.request({
        url: baseUrl + "/System/User/sale_flow_set",
        header: {
          'Content-Type': 'application/json'        
        },
        data:{
          sale: that.data.sale,
          uid:that.data.uid,
          values: e.detail.value,
        },
        method: "POST", 
        success: function (res) { 
        if(res.code>0) {
          wx.showToast({
            title: res.data.msg,
            icon: 'success',
            duration: 1000
          }) 

        } else {

          wx.showToast({ 
            title: res.data.msg, 
            icon: 'success', 
            duration: 1000 
          })

        }
      }
      }) 
    },
//  改变业务员的值
  setFlow:function(e){ 
    var that = this; 
    var sale_flow = e.detail.value;
    const index = e.target.dataset.index;
    var list = that.data.list;
    var total = (1 - list[index]['shop_flow']);
    var manager_flow = that.accSub(total,sale_flow); 
    //修改值
    list[index]['sale_flow'] = sale_flow;
    list[index]['manager_flow'] = manager_flow; 
    this.setData({
      list: list
    });
  },

//  改变业务经理的值
  setManager:function(e){
    var that = this;
    var manager_flow = e.detail.value;
    const index = e.target.dataset.index;
    var list = that.data.list;
    var total = (1 - list[index]['shop_flow']);
    var sale_flow = that.accSub(total, manager_flow);
    //修改值
    list[index]['sale_flow'] = sale_flow;
    list[index]['manager_flow'] = manager_flow;
    this.setData({
      list: list
    });
  },

  // （全选） 改变业务经理的值
  setAllSale: function (e) {
    var that = this;
    const showid = that.data.showid;
    var sale_flow = e.detail.value;
    if (showid == 1) {
      var shop_flow = that.data.shop_flow;
      var total = (1 - shop_flow);
      var manager_flow = that.accSub(total, sale_flow);
      var list = that.data.list;
      for (var i = 0; i < list.length; i++) {
        list[i]['manager_flow'] = manager_flow;
        list[i]['sale_flow'] = sale_flow;
      }
      this.setData({
        manager_flow: manager_flow,
        list: list
      });
    }
  },
  // （全选） 改变业务经理的值
  setAllManager: function (e) {
    var that = this;
    const showid = that.data.showid;
    var manager_flow = e.detail.value;
    if(showid == 1){
      var shop_flow = that.data.shop_flow;
      var total = (1 - shop_flow);
      var sale_flow = that.accSub(total, manager_flow);
      var list = that.data.list;
      for(var i=0; i<list.length;i++){
        list[i]['manager_flow'] = manager_flow;
        list[i]['sale_flow'] = sale_flow;
      } 
      this.setData({
        sale_flow: sale_flow,
        list: list
      });
    } 
  },

  /**
   ** 减法函数，用来得到精确的减法结果
   ** 说明：javascript的减法结果会有误差，在两个浮点数相减的时候会比较明显。这个函数返回较为精确的减法结果。
   ** 调用：accSub(arg1,arg2)
   ** 返回值：arg1加上arg2的精确结果
   **/
  accSub:function(arg1, arg2) {
    var r1, r2, m, n;
    try {
      r1 = arg1.toString().split(".")[1].length;
    }
    catch (e) {
      r1 = 0;
    }
    try {
      r2 = arg2.toString().split(".")[1].length;
    }
    catch (e) {
      r2 = 0;
    }
    m = Math.pow(10, Math.max(r1, r2)); //last modify by deeka //动态控制精度长度
    n = (r1 >= r2) ? r1 : r2;
    return((arg1 * m - arg2 * m) / m).toFixed(n);
  },



  
})
